//
//  YAYISendResumeDetailViewController.m
//  YAYIDoctor
//
//  Created by Veer on 16/7/1.
//
//

#import "YAYISendResumeDetailViewController.h"

#import "YAYIScrolImageView.h"
#import "YAYIRecruitHospitalModel.h"
#import "NSString+Tools.h"


@interface YAYISendResumeDetailViewController () <UIScrollViewDelegate>

@property (nonatomic, assign) CGRect desImageViewRect;
@property (nonatomic, copy) UIImage *image;

@property (nonatomic, weak) YAYIScrolImageView *scrollImageView;
@property (nonatomic, weak) UIScrollView *scrollView;
@property (nonatomic, weak) UIView *containView;
@property (nonatomic, weak) UIView *lastView;

@property (nonatomic, weak) UILabel *hospitalNameLbl;
@property (nonatomic, weak) UILabel *addressLbl;
@property (nonatomic, weak) UILabel *distanceLbl;
@property (nonatomic, weak) UILabel *briefLbl;
@property (nonatomic, weak) UILabel *jobLbl;

@end

@implementation YAYISendResumeDetailViewController

- (instancetype)initWithModel:(UIImage *)image desImageViewRect:(CGRect)desRect{
    if (self = [super init]) {
        self.desImageViewRect = desRect;
        self.image = image;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"职位详情";
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
//    [self setupNav];
    [self setupSubView];
    [self setupHospitalDetailView];
    [self getJobDetail];
}


- (void)setupNav
{
    // 改写导航栏返回按钮执行方法
    UIButton *backBtn = self.navigationItem.leftBarButtonItem.customView;
    
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
}


- (void)setupSubView
{
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.alwaysBounceVertical = YES;
    scrollView.delegate = self;
    scrollView.frame = CGRectMake(0, 0, SCREEN_width, SCREEN_height);
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    
    CGRect rect = CGRectMake(0, 64, SCREEN_width, 310 * YAYIScreenScale);
//    rect.origin.y = 0;
    UIView *headerView = [[UIView alloc] initWithFrame:rect];
    headerView.frame = rect;
    [scrollView addSubview:headerView];
    
    rect.origin.y = 0;
    YAYIScrolImageView *scrollImageView = [[YAYIScrolImageView alloc] initWithFrame:rect];
    scrollImageView.isAutoScroll = NO;
    [headerView addSubview:scrollImageView];
    self.scrollImageView = scrollImageView;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
    [scrollImageView addGestureRecognizer:tap];
    
    
    UIView *containView = [[UIView alloc] init];
    containView.backgroundColor = [UIColor whiteColor];
    containView.frame = CGRectMake(0,  CGRectGetMaxY(rect) + 64, SCREEN_width, 300);
    [scrollView addSubview:containView];
    self.containView = containView;

}


- (void)setupHospitalDetailView
{
    
    //  医院信息label
    UILabel *hospitalNameLbl = [[UILabel alloc] init];
    hospitalNameLbl.text = self.recruitHospitalModel.hospitalname;
    hospitalNameLbl.font = [UIFont systemFontOfSize:16];
    [self.containView addSubview:hospitalNameLbl];
    self.hospitalNameLbl = hospitalNameLbl;
    [hospitalNameLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(10);
    }];
    
    UILabel *addressLbl = [[UILabel alloc] init];
    addressLbl.text = self.recruitHospitalModel.hosaddress;
    addressLbl.numberOfLines = 0;
    addressLbl.font = [UIFont systemFontOfSize:14];
    addressLbl.textColor = YAYIColor(156, 156, 156);
    [self.containView addSubview:addressLbl];
    self.addressLbl = addressLbl;
    [addressLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_lessThanOrEqualTo(-70);
        make.top.mas_equalTo(hospitalNameLbl.mas_bottom).offset(5);
    }];
    
    UILabel *distanceLbl = [[UILabel alloc] init];
    distanceLbl.text = [self getDistanceStringByDistance:self.recruitHospitalModel.distance];
    distanceLbl.font = [UIFont systemFontOfSize:14];
    distanceLbl.textColor = YAYIColor(156, 156, 156);
    [self.containView addSubview:distanceLbl];
    self.distanceLbl = distanceLbl;
    [distanceLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(addressLbl);
    }];
    
    // 线
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = YAYIColor(230, 230, 230);
    [self.containView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(addressLbl.mas_bottom).offset(8);
        make.width.mas_equalTo(SCREEN_width);
        make.height.mas_equalTo(0.5);
    }];
    
    
    // 标题
    UILabel *titleLbl1 = [[UILabel alloc] init];
    titleLbl1.text = @"医院简介";
    titleLbl1.font = [UIFont systemFontOfSize:16];
    titleLbl1.textColor = YAYIColor(96, 167, 237);
    [self.containView addSubview:titleLbl1];
    [titleLbl1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(lineView.mas_bottom).offset(10);
    }];
    
    UILabel *briefLbl = [[UILabel alloc] init];
    briefLbl.text = self.recruitHospitalModel.brief;
    briefLbl.numberOfLines = 0;
    briefLbl.font = [UIFont systemFontOfSize:14];
    briefLbl.textColor = YAYIColor(141, 142, 143);
    [self.containView addSubview:briefLbl];
    self.briefLbl = briefLbl;
    [briefLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(titleLbl1.mas_bottom).offset(8);
        make.right.mas_equalTo(-15);
    }];
    
    
    // 合作信息
    UILabel *titleLbl2 = [[UILabel alloc] init];
    titleLbl2.text = @"合作信息";
    titleLbl2.numberOfLines = 0;
    titleLbl2.font = [UIFont systemFontOfSize:16];
    titleLbl2.textColor = YAYIColor(96, 167, 237);
    [self.containView addSubview:titleLbl2];
    [titleLbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.top.mas_equalTo(briefLbl.mas_bottom).offset(10);
    }];
    
    YAYILog(@"岗位职责：%@", self.recruitHospitalModel.jobtitle);
    UILabel *jobLbl = [[UILabel alloc] init];
    jobLbl.text = self.recruitHospitalModel.jobtitle;
    jobLbl.numberOfLines = 0;
    jobLbl.font = [UIFont systemFontOfSize:14];
    jobLbl.textColor = YAYIColor(141, 142, 143);
    [self.containView addSubview:jobLbl];
    self.jobLbl = jobLbl;
    [jobLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.top.mas_equalTo(titleLbl2.mas_bottom).offset(8);
    }];
}


#pragma mark - 获取数据
- (void)getJobDetail
{

    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"jobid"] = self.jobid;
    NSDictionary *locDcit = [[NSUserDefaults standardUserDefaults] dictionaryForKey:YAYILocationKey];
    if (locDcit) {
        param[@"pointX"] = locDcit[@"longitude"];
        param[@"pointY"] = locDcit[@"latitude"];
    }

    [YAYINetWorking getWithURL:jobDetail_url param:param finish:^(NSData *data, NSDictionary *dict, NSError *error) {

        YAYILog(@"返回:%@", dict);
        if (nil == error) {
            
            YAYIRecruitHospitalModel *jobModel = [[YAYIRecruitHospitalModel alloc] init];
            [jobModel setValuesForKeysWithDictionary:dict[@"data"][0]];
            
            // 设置数据
            self.hospitalNameLbl.text = jobModel.hospitalname;
            self.addressLbl.text = jobModel.hosaddress;
            self.distanceLbl.text = [self getDistanceStringByDistance:jobModel.distance];
            self.briefLbl.text = jobModel.brief;
            self.jobLbl.text = jobModel.jobtitle;
            
            
            NSMutableArray *urlArray = [NSMutableArray array];
            for (NSString *str in jobModel.images) {
                
                NSString *urlStr = [NSString stringWithFormat:@"%@%@", API, str];
                [urlArray addObject:urlStr];
            }
            self.scrollImageView.imageAry = [urlArray copy];
            
            // 计算containView的高度，和contentSize
            CGFloat contentViewH = self.lastView.y + self.lastView.height + 20;
            if (contentViewH < SCREEN_height - 64 - self.desImageViewRect.size.height) {
                
                contentViewH = SCREEN_height - 64 - self.desImageViewRect.size.height;
            }
            self.containView.height = contentViewH;
            self.scrollView.contentSize = CGSizeMake(SCREEN_width, 64 + self.containView.height + self.desImageViewRect.size.height);
        }

    }];
}


#pragma mark - 点击事件


- (void)handleTapGesture:(UITapGestureRecognizer *)recognizer
{
    
    self.view.backgroundColor = [UIColor clearColor];
    
    self.scrollImageView.hidden = YES;
    __block typeof(self) weakself = self;
    [UIView animateWithDuration:0.3 animations:^{
        
        weakself.containView.y = SCREEN_height;
    }];
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)onNavBtnClick:(UIButton *)sender
{
    
    self.view.backgroundColor = [UIColor clearColor];
    self.scrollImageView.hidden = YES;
    __block typeof(self) weakself = self;
    
    [UIView animateWithDuration:0.3 animations:^{
        
        weakself.containView.y = SCREEN_height;
    } completion:^(BOOL finished) {
        
    }];
    [weakself.navigationController popViewControllerAnimated:YES];
}


#pragma mark - 工具方法
- (NSString *)getDistanceStringByDistance:(NSInteger)distance
{
    NSString *distanceString = @"";
    
    if (distance < 1000){
        
        distanceString = [NSString stringWithFormat:@"< %.1fkm", 1.0];
    }else if (distance < 10000){
        
        CGFloat km = distance / 1000.0;
        distanceString = [NSString stringWithFormat:@"< %0.1fkm", km];
    }else{
        
        NSInteger km = (NSInteger)((distance + 500) / 1000);
        distanceString = [NSString stringWithFormat:@"< %zdkm", km + 1];
    }
    
    
    return distanceString;
}


#pragma mark - 代理方法
-(void)didFinishTransition
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.scrollView.contentSize = CGSizeMake(SCREEN_width, 2 * SCREEN_height);
    
    NSMutableArray *urlArray = [NSMutableArray array];
    for (NSString *str in self.recruitHospitalModel.images) {
        
        NSString *urlStr = [NSString stringWithFormat:@"%@%@", API, str];
        [urlArray addObject:urlStr];
    }
    //    NSArray *imgStrArray = @[@"photo_sample_01", @"photo_sample_02", @"photo_sample_03", @"photo_sample_04", @"photo_sample_05"];
    self.scrollImageView.imageAry = [urlArray copy];
    
    // 计算containView的高度，和contentSize
    CGFloat contentViewH = self.lastView.y + self.lastView.height + 70;
    if (contentViewH < SCREEN_height - 64 - self.desImageViewRect.size.height) {
        
        contentViewH = SCREEN_height - 64 - self.desImageViewRect.size.height;
    }
    self.containView.height = contentViewH;
    self.scrollView.contentSize = CGSizeMake(SCREEN_width, 64 + self.containView.height + self.desImageViewRect.size.height);
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY < 0) {
        
        offsetY = ABS(offsetY);
        CGFloat scale = (self.scrollImageView.height + offsetY) / self.scrollImageView.height;
        self.scrollImageView.transform = CGAffineTransformMakeScale(scale, scale);
        self.scrollImageView.y = - offsetY;
    }
}


@end
